package tyk.Servlet;

import tyk.Service.EmailService;
import tyk.Service.UserService;
import tyk.pojo.User;
import tyk.utils.SendEmail;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 *@title $NAME
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/28 11:39
 **/
public class EmailServlet extends HttpServlet {
    public void EmailBind(HttpServletRequest request,HttpServletResponse response) throws IOException, SQLException, ServletException {
        System.out.println("进入测试");
        String ToEmail=request.getParameter("ToEmail");
        String user_name=request.getParameter("user_name");
        String sql="select id,username,password,email from user where email = ?;";
        Integer rows=EmailService.executeQuery(sql,ToEmail);
        if(rows>0){
            request.setAttribute("status"," 该邮箱已被其它账号绑定，请换一个邮箱号");
            request.getRequestDispatcher("./html/Email.jsp").forward(request,response);
            return;
        }
        response.sendRedirect("./html/SendSuccessEmail.jsp");
        SendEmail sendEmail=new SendEmail();
        sendEmail.setToEmail(ToEmail);
        sendEmail.setUser_name(user_name);
        sendEmail.run1();
    }
    public void EmailActivation(HttpServletRequest request,HttpServletResponse response) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException, IOException {
        System.out.println("Hello World");
        Integer token= Integer.valueOf(request.getParameter("token"));
        String user_name=request.getParameter("user_name");
        String email=request.getParameter("email");
        System.out.println(token+user_name+email);
        EmailService emailService=new EmailService();
        Integer rows=emailService.SelectEmail(user_name,token);
        System.out.println("rows="+rows);
        if(rows==0) response.sendRedirect("./html/Fail.jsp");
        else{
            emailService.EditEmail(user_name,email);
            response.sendRedirect("./html/EditEmailSuccess.jsp");
            request.getSession().removeAttribute("User");
        }
    }
    public void SelectByEmail(HttpServletRequest request,HttpServletResponse response) throws SQLException, ServletException, IOException, IllegalAccessException, NoSuchFieldException, InstantiationException {
        String ToEmail=request.getParameter("ToEmail");
        EmailService emailService=new EmailService();
        List<User> list=new ArrayList<>();
        list=emailService.SelectByEmail(ToEmail);
        if(list.size()==0) {
            request.setAttribute("status","该邮箱号未绑定任何账号，无法进行找回");
            request.setAttribute("ToEmail",ToEmail);
            request.getRequestDispatcher("./html/RetrievePassword.jsp").forward(request,response);
        }else {
            SendEmail sendEmail=new SendEmail();
            sendEmail.setToEmail(ToEmail);
            sendEmail.setUser_name(list.get(0).getUsername());
            sendEmail.run2();
            response.sendRedirect("./html/SendSuccessEmail.jsp");
        }
    }
    public void EditPassword(HttpServletRequest request,HttpServletResponse response) throws SQLException, NoSuchFieldException, InstantiationException, IllegalAccessException, IOException, ServletException {
        System.out.println("Hello World");
        Integer token= Integer.valueOf(request.getParameter("token"));
        String user_name=request.getParameter("user_name");
        EmailService emailService=new EmailService();
        Integer rows=emailService.SelectEmail(user_name,token);
        if(rows==0) response.sendRedirect("./html/Fail.jsp");
        else{
            request.getRequestDispatcher("./html/EditPassword.jsp").forward(request,response);
        }
    }
    public void SuccessPassword(HttpServletRequest request,HttpServletResponse response) throws SQLException, NoSuchAlgorithmException, IOException {
        String user_name=request.getParameter("user_name");
        String password=request.getParameter("password");
        EmailService emailService=new EmailService();
        emailService.SuccessPassword(user_name,password);
        response.sendRedirect("./html/SuccessEditPassword.jsp");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
